﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fideeraSoft.Handlers;
using System.Windows.Forms;
using System.Drawing;
using fideeraSoft.Utils;
using System.Data;
using System.Data.SqlClient;
using fideeraSoft.Entities;

namespace fideeraSoft.Paneles
{
    public class RemitoSalidaPanel:PanelGeneral
    {
        private System.Windows.Forms.Button btFinalizar;
        private System.Windows.Forms.Button btBuscarProduccionId;
        private System.Windows.Forms.TableLayoutPanel tlpBuscarEnvasado;
        private System.Windows.Forms.TableLayoutPanel tlpBotones;
        private System.Windows.Forms.Button btVolver;
        private System.Windows.Forms.DataGridView dgvSalida;
        private System.Windows.Forms.DateTimePicker dtpFechaFin;
        private System.Windows.Forms.Label lbFechaFin;
        private System.Windows.Forms.TextBox tbBuscarEnvasado;

        private int posicionDataGrid=0;
        private int posicionBuscarProd = 0;
        private int posicionGroupBox = 0;
        private int posicionBuscarEntreFechas = 0;
        
        private SqlDataAdapter adapter;
        private DataSet ds;
        private DataSet changes;
        private TableLayoutPanel tlpBuscarEntreFechas;
        private DateTimePicker dtpBuscarIni;
        private Button btBuscarEntreFecha;
        private DateTimePicker dtpBuscarFin;
        private SqlCommandBuilder cmdBuilder;
        private GroupBox gbFechaFin;
        private TableLayoutPanel tlpGral;
        private Label lbIdRemito;
        private Label lbId;
        private TextBox tbCodigo;
        private Label lbCodigo;
        private Label lbUsuario;
        private TextBox tbCantPaquetesSalida;
        private Label lbCantPaquetesSalida;
        private Button btAgregarProduccion;
        
        private ListBox lsbProducciones;
       
        private TableLayoutPanel tlpAgregarProd;
        private Button btRemoverProduccion;
        private Label lbUserName;

        private List<Produccion> lstProducciones;
        
        public RemitoSalidaPanel(Handler handler)
        {

            lstProducciones = new List<Produccion>();

            this.formatearPanelGeneral(handler);

            this.formatearTableTitulo("REMITO DE SALIDA");

            this.InitializeComponent();

             //string query = "SELECT ID AS ID_Producción, producto AS ID_Producto,fechaFinEnvasado AS FechaFinEnvasado,cantidadPaquetes AS CantidadPaquetes, observacion AS Observación FROM Produccion WHERE fechaSalida IS NULL AND fechaFinEnvasado IS NOT NULL";
            string query = "SELECT pr.ID AS [ID Producción], p.nombre AS [Nombre del Producto], pr.fechaFinEnvasado AS [Fecha Fin de Envasado],pr.cantidadPaquetes AS [Cantidad de Paquetes], pr.observacion AS Observación1,pr.observacionFinal AS Observación2,e.observacion AS Observación3,e.observacionFin AS Observación4 FROM Produccion AS pr INNER JOIN Producto AS p ON p.ID=pr.producto INNER JOIN Envasado AS e ON e.produccion=pr.ID WHERE pr.fechaFinEnvasado IS NOT NULL AND pr.cantidadPaquetes>'0'";

            adapter = SQLclass.obtenerAdapter(query);
            if (adapter != null)
            {
                ds = new DataSet();
                adapter.Fill(ds);

                if(ds.Tables[0].Rows.Count<=0)
                    MessageBox.Show("NO HAY NINGUNA PRODUCCIÓN ENVASADA FINALIZADA CARGADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                else
                     dgvSalida.DataSource = ds.Tables[0];

                this.formatearDataGrid();
                this.formatearTablaBuscarProd();
                formatearTablaBuscarEntreFecha();
                
                formatearTablaGral();    
                formatearTablaGroupBox();
                            
                this.formatearTablaBotones();



                this.Controls.Add(dgvSalida);
                this.Controls.Add(tlpBuscarEnvasado);

                this.Controls.Add(tlpBuscarEntreFechas);

                this.Controls.Add(gbFechaFin);

                this.Controls.Add(tlpBotones);

            }
            else
            {
                //CLogger.Append("[ProveedorPanelMostrar] Lista Proveedor mostrar Null: ", CLogger.INFO);      //DEBUG
                MessageBox.Show("NO HAY NINGUNA PRODUCCIÓN ENVASADA FINALIZADA CARGADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.handler.backToPrincipal();
            }
            
        }


        #region FORMATEAR TABLAS

        private void formatearDataGrid()
        {

            posicionDataGrid = this.InitialRealPointVertical;

            dgvSalida.AutoGenerateColumns = true;
            dgvSalida.Size = new Size(this.Size.Width - 100, 180);
            dgvSalida.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
            dgvSalida.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllHeaders);
            dgvSalida.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            

            dgvSalida.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, dgvSalida.Size.Width), posicionDataGrid);
        }

        private void formatearTablaBuscarProd()
        {
            posicionBuscarProd= posicionDataGrid+dgvSalida.Size.Height+20;

            tlpBuscarEnvasado.Controls.Add(btBuscarProduccionId);
            tlpBuscarEnvasado.Controls.Add(tbBuscarEnvasado);
                        
            //tlpBuscarProduccion.Size = new Size(400, tbBuscarProduccion.Size.Height + 9);
            tlpBuscarEnvasado.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, this.tlpBuscarEnvasado.Size.Width), posicionBuscarProd);


        }

        private void formatearTablaBuscarEntreFecha()
        {
            posicionBuscarEntreFechas = posicionBuscarProd + tlpBuscarEnvasado.Size.Height + 5;

            tlpBuscarEntreFechas.Controls.Add(btBuscarEntreFecha);
            dtpBuscarIni.Value = System.DateTime.Now;
            dtpBuscarFin.Value = System.DateTime.Now;

            tlpBuscarEntreFechas.Controls.Add(dtpBuscarIni);
            tlpBuscarEntreFechas.Controls.Add(dtpBuscarFin);

            tlpBuscarEntreFechas.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, this.tlpBuscarEntreFechas.Size.Width), posicionBuscarEntreFechas);


        }
        
        private void formatearTablaGroupBox()
        {
            posicionGroupBox = posicionBuscarEntreFechas + tlpBuscarEntreFechas.Size.Height + 30;
            
            gbFechaFin.Controls.Add(tlpGral);

            gbFechaFin.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, this.gbFechaFin.Size.Width), posicionGroupBox);

            

        }

        private void formatearTablaGral()
        {

            tlpGral.Location = new Point(MetodosGenerales.calcularCenterWidth(gbFechaFin.Size.Width, tlpGral.Size.Width), 30);
            
            cargarControlesEnTabla();

        }

        private void cargarControlesEnTabla()
        {
            tlpGral.Controls.Add(lbId);
                        
            lbIdRemito.Text = this.handler.buscarNuevoUltimoIdRemitoSalidaEnBD().ToString();
            tlpGral.Controls.Add(lbIdRemito);
            
            tlpGral.Controls.Add(lbCodigo);
            tlpGral.Controls.Add(tbCodigo);


            tlpGral.Controls.Add(lbFechaFin);
            dtpFechaFin.Value = System.DateTime.Now;
            tlpGral.Controls.Add(dtpFechaFin);
                        
            tlpGral.Controls.Add(lbUsuario);
            lbUserName.Text = UsuarioLoggin.username;
            tlpGral.Controls.Add(lbUserName);

            tlpGral.Controls.Add(lbCantPaquetesSalida);
            tlpGral.Controls.Add(tbCantPaquetesSalida);

            tlpAgregarProd.Controls.Add(lsbProducciones);
            tlpAgregarProd.Controls.Add(btRemoverProduccion);
            tlpGral.Controls.Add(btAgregarProduccion);
            tlpGral.Controls.Add(tlpAgregarProd);
        }


        private void formatearTablaBotones()
        {
            tlpBotones.Controls.Add(btVolver);
            tlpBotones.Controls.Add(btFinalizar);
            
            //tlpBotones.Size = new Size(400, tbBuscarProvCuil.Size.Height + 9);
            tlpBotones.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, this.tlpBotones.Size.Width), posicionGroupBox + gbFechaFin.Size.Height + 30);

        }

        #endregion

        #region INICIALIZAR
        private void InitializeComponent()
        {
            this.btFinalizar = new System.Windows.Forms.Button();
            this.btBuscarProduccionId = new System.Windows.Forms.Button();
            this.tlpBuscarEnvasado = new System.Windows.Forms.TableLayoutPanel();
            this.tlpBotones = new System.Windows.Forms.TableLayoutPanel();
            this.btVolver = new System.Windows.Forms.Button();
            this.dgvSalida = new System.Windows.Forms.DataGridView();
            this.tbBuscarEnvasado = new System.Windows.Forms.TextBox();
            this.dtpFechaFin = new System.Windows.Forms.DateTimePicker();
            this.lbFechaFin = new System.Windows.Forms.Label();
            this.tlpBuscarEntreFechas = new System.Windows.Forms.TableLayoutPanel();
            this.dtpBuscarIni = new System.Windows.Forms.DateTimePicker();
            this.btBuscarEntreFecha = new System.Windows.Forms.Button();
            this.dtpBuscarFin = new System.Windows.Forms.DateTimePicker();
            this.gbFechaFin = new System.Windows.Forms.GroupBox();
            this.tlpGral = new System.Windows.Forms.TableLayoutPanel();
            this.lbIdRemito = new System.Windows.Forms.Label();
            this.lbId = new System.Windows.Forms.Label();
            this.tbCodigo = new System.Windows.Forms.TextBox();
            this.lbCodigo = new System.Windows.Forms.Label();
            this.lbUsuario = new System.Windows.Forms.Label();
            this.lbUserName = new System.Windows.Forms.Label();
            this.tbCantPaquetesSalida = new System.Windows.Forms.TextBox();
            this.lbCantPaquetesSalida = new System.Windows.Forms.Label();
            this.btAgregarProduccion = new System.Windows.Forms.Button();
            this.lsbProducciones = new System.Windows.Forms.ListBox();
            this.tlpAgregarProd = new System.Windows.Forms.TableLayoutPanel();
            this.btRemoverProduccion = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dgvSalida)).BeginInit();
            this.SuspendLayout();
            // 
            // btFinalizar
            // 
            this.btFinalizar.BackColor = System.Drawing.Color.AliceBlue;
            this.btFinalizar.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btFinalizar.Location = new System.Drawing.Point(0, 0);
            this.btFinalizar.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btFinalizar.Name = "btFinalizar";
            this.btFinalizar.Size = new System.Drawing.Size(75, 40);
            this.btFinalizar.TabIndex = 0;
            this.btFinalizar.Text = "Finalizar";
            this.btFinalizar.UseVisualStyleBackColor = false;
            this.btFinalizar.Click += new System.EventHandler(this.btFinalizar_Click);
            // 
            // btBuscarProduccionId
            // 
            this.btBuscarProduccionId.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarProduccionId.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarProduccionId.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarProduccionId.Location = new System.Drawing.Point(0, 0);
            this.btBuscarProduccionId.Name = "btBuscarProduccionId";
            this.btBuscarProduccionId.Size = new System.Drawing.Size(75, 23);
            this.btBuscarProduccionId.TabIndex = 0;
            this.btBuscarProduccionId.Text = "Buscar Produccion (ID)";
            this.btBuscarProduccionId.UseVisualStyleBackColor = false;
            this.btBuscarProduccionId.Click += new System.EventHandler(this.btBuscarProduccionEnvasadaFin_Click);
            // 
            // tlpBuscarEnvasado
            // 
            this.tlpBuscarEnvasado.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBuscarEnvasado.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarEnvasado.ColumnCount = 2;
            this.tlpBuscarEnvasado.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarEnvasado.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarEnvasado.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarEnvasado.Name = "tlpBuscarEnvasado";
            this.tlpBuscarEnvasado.RowCount = 1;
            this.tlpBuscarEnvasado.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarEnvasado.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarEnvasado.Size = new System.Drawing.Size(460, 40);
            this.tlpBuscarEnvasado.TabIndex = 0;
            // 
            // tlpBotones
            // 
            this.tlpBotones.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBotones.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBotones.ColumnCount = 2;
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Location = new System.Drawing.Point(0, 0);
            this.tlpBotones.Name = "tlpBotones";
            this.tlpBotones.RowCount = 1;
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBotones.Size = new System.Drawing.Size(200, 46);
            this.tlpBotones.TabIndex = 0;
            // 
            // btVolver
            // 
            this.btVolver.BackColor = System.Drawing.Color.AliceBlue;
            this.btVolver.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btVolver.Location = new System.Drawing.Point(0, 0);
            this.btVolver.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btVolver.Name = "btVolver";
            this.btVolver.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btVolver.Size = new System.Drawing.Size(75, 40);
            this.btVolver.TabIndex = 0;
            this.btVolver.Text = "Volver";
            this.btVolver.UseVisualStyleBackColor = false;
            this.btVolver.Click += new System.EventHandler(this.btVolver_Click);
            // 
            // dgvSalida
            // 
            this.dgvSalida.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.dgvSalida.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvSalida.Location = new System.Drawing.Point(0, 0);
            this.dgvSalida.Name = "dgvSalida";
            this.dgvSalida.ReadOnly = true;
            this.dgvSalida.Size = new System.Drawing.Size(240, 90);
            this.dgvSalida.TabIndex = 0;
            this.dgvSalida.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvProduccionFin_CellClick);
            // 
            // tbBuscarEnvasado
            // 
            this.tbBuscarEnvasado.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbBuscarEnvasado.Location = new System.Drawing.Point(0, 0);
            this.tbBuscarEnvasado.MaxLength = 10;
            this.tbBuscarEnvasado.Name = "tbBuscarEnvasado";
            this.tbBuscarEnvasado.Size = new System.Drawing.Size(200, 30);
            this.tbBuscarEnvasado.TabIndex = 0;
            // 
            // dtpFechaFin
            // 
            this.dtpFechaFin.CalendarFont = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dtpFechaFin.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dtpFechaFin.Location = new System.Drawing.Point(0, 0);
            this.dtpFechaFin.Name = "dtpFechaFin";
            this.dtpFechaFin.Size = new System.Drawing.Size(300, 30);
            this.dtpFechaFin.TabIndex = 0;
            // 
            // lbFechaFin
            // 
            this.lbFechaFin.AutoSize = true;
            this.lbFechaFin.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbFechaFin.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbFechaFin.Location = new System.Drawing.Point(0, 0);
            this.lbFechaFin.Name = "lbFechaFin";
            this.lbFechaFin.Size = new System.Drawing.Size(100, 23);
            this.lbFechaFin.TabIndex = 0;
            this.lbFechaFin.Text = "Fecha Remito Salida";
            this.lbFechaFin.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tlpBuscarEntreFechas
            // 
            this.tlpBuscarEntreFechas.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBuscarEntreFechas.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarEntreFechas.ColumnCount = 3;
            this.tlpBuscarEntreFechas.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
            this.tlpBuscarEntreFechas.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 37.5F));
            this.tlpBuscarEntreFechas.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 37.5F));
            this.tlpBuscarEntreFechas.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarEntreFechas.Name = "tlpBuscarEntreFechas";
            this.tlpBuscarEntreFechas.RowCount = 1;
            this.tlpBuscarEntreFechas.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBuscarEntreFechas.Size = new System.Drawing.Size(600, 40);
            this.tlpBuscarEntreFechas.TabIndex = 0;
            // 
            // dtpBuscarIni
            // 
            this.dtpBuscarIni.CalendarFont = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dtpBuscarIni.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dtpBuscarIni.Location = new System.Drawing.Point(0, 0);
            this.dtpBuscarIni.Margin = new System.Windows.Forms.Padding(3, 7, 3, 3);
            this.dtpBuscarIni.Name = "dtpBuscarIni";
            this.dtpBuscarIni.Size = new System.Drawing.Size(300, 23);
            this.dtpBuscarIni.TabIndex = 0;
            // 
            // btBuscarEntreFecha
            // 
            this.btBuscarEntreFecha.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarEntreFecha.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarEntreFecha.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarEntreFecha.Location = new System.Drawing.Point(0, 0);
            this.btBuscarEntreFecha.Name = "btBuscarEntreFecha";
            this.btBuscarEntreFecha.Size = new System.Drawing.Size(75, 23);
            this.btBuscarEntreFecha.TabIndex = 0;
            this.btBuscarEntreFecha.Text = "Buscar Entre Fechas";
            this.btBuscarEntreFecha.UseVisualStyleBackColor = false;
            this.btBuscarEntreFecha.Click += new System.EventHandler(this.btBuscarEntreFecha_Click);
            // 
            // dtpBuscarFin
            // 
            this.dtpBuscarFin.CalendarFont = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dtpBuscarFin.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dtpBuscarFin.Location = new System.Drawing.Point(0, 0);
            this.dtpBuscarFin.Margin = new System.Windows.Forms.Padding(3, 7, 3, 3);
            this.dtpBuscarFin.Name = "dtpBuscarFin";
            this.dtpBuscarFin.Size = new System.Drawing.Size(300, 23);
            this.dtpBuscarFin.TabIndex = 0;
            // 
            // gbFechaFin
            // 
            this.gbFechaFin.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.gbFechaFin.Location = new System.Drawing.Point(0, 0);
            this.gbFechaFin.Name = "gbFechaFin";
            this.gbFechaFin.Size = new System.Drawing.Size(700, 400);
            this.gbFechaFin.TabIndex = 0;
            this.gbFechaFin.TabStop = false;
            this.gbFechaFin.Text = "Remito de Salida";
            // 
            // tlpGral
            // 
            this.tlpGral.BackColor = System.Drawing.Color.LightCyan;
            this.tlpGral.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpGral.ColumnCount = 2;
            this.tlpGral.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 30F));
            this.tlpGral.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 70F));
            this.tlpGral.Location = new System.Drawing.Point(0, 0);
            this.tlpGral.Name = "tlpGral";
            this.tlpGral.RowCount = 6;
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 12F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 12F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 12F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 12F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 12F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 40F));
            this.tlpGral.Size = new System.Drawing.Size(650, 350);
            this.tlpGral.TabIndex = 0;
            // 
            // lbIdRemito
            // 
            this.lbIdRemito.AutoSize = true;
            this.lbIdRemito.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbIdRemito.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbIdRemito.Location = new System.Drawing.Point(4, 1);
            this.lbIdRemito.Name = "lbIdRemito";
            this.lbIdRemito.Size = new System.Drawing.Size(317, 50);
            this.lbIdRemito.TabIndex = 3;
            this.lbIdRemito.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // lbId
            // 
            this.lbId.AutoSize = true;
            this.lbId.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbId.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbId.Location = new System.Drawing.Point(4, 229);
            this.lbId.Name = "lbId";
            this.lbId.Size = new System.Drawing.Size(317, 20);
            this.lbId.TabIndex = 4;
            this.lbId.Text = "ID Remito Salida";
            this.lbId.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tbCodigo
            // 
            this.tbCodigo.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbCodigo.Location = new System.Drawing.Point(328, 211);
            this.tbCodigo.MaxLength = 10;
            this.tbCodigo.Name = "tbCodigo";
            this.tbCodigo.Size = new System.Drawing.Size(200, 30);
            this.tbCodigo.TabIndex = 1;
            // 
            // lbCodigo
            // 
            this.lbCodigo.AutoSize = true;
            this.lbCodigo.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbCodigo.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbCodigo.Location = new System.Drawing.Point(4, 208);
            this.lbCodigo.Name = "lbCodigo";
            this.lbCodigo.Size = new System.Drawing.Size(317, 20);
            this.lbCodigo.TabIndex = 2;
            this.lbCodigo.Text = "Código Remito";
            this.lbCodigo.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lbUsuario
            // 
            this.lbUsuario.AutoSize = true;
            this.lbUsuario.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbUsuario.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbUsuario.Location = new System.Drawing.Point(0, 0);
            this.lbUsuario.Name = "lbUsuario";
            this.lbUsuario.Size = new System.Drawing.Size(100, 23);
            this.lbUsuario.TabIndex = 0;
            this.lbUsuario.Text = "Usuario";
            this.lbUsuario.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lbUserName
            // 
            this.lbUserName.AutoSize = true;
            this.lbUserName.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbUserName.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbUserName.Location = new System.Drawing.Point(0, 0);
            this.lbUserName.Name = "lbUserName";
            this.lbUserName.Size = new System.Drawing.Size(100, 23);
            this.lbUserName.TabIndex = 0;
            this.lbUserName.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // tbCantPaquetesSalida
            // 
            this.tbCantPaquetesSalida.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbCantPaquetesSalida.Location = new System.Drawing.Point(328, 211);
            this.tbCantPaquetesSalida.MaxLength = 10;
            this.tbCantPaquetesSalida.Name = "tbCantPaquetesSalida";
            this.tbCantPaquetesSalida.Size = new System.Drawing.Size(120, 30);
            this.tbCantPaquetesSalida.TabIndex = 1;
            // 
            // lbCantPaquetesSalida
            // 
            this.lbCantPaquetesSalida.AutoSize = true;
            this.lbCantPaquetesSalida.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbCantPaquetesSalida.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbCantPaquetesSalida.Location = new System.Drawing.Point(0, 0);
            this.lbCantPaquetesSalida.Name = "lbCantPaquetesSalida";
            this.lbCantPaquetesSalida.Size = new System.Drawing.Size(100, 23);
            this.lbCantPaquetesSalida.TabIndex = 0;
            this.lbCantPaquetesSalida.Text = "Cant. Paquetes";
            this.lbCantPaquetesSalida.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // btAgregarProduccion
            // 
            this.btAgregarProduccion.BackColor = System.Drawing.Color.AliceBlue;
            this.btAgregarProduccion.Dock = System.Windows.Forms.DockStyle.Top;
            this.btAgregarProduccion.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btAgregarProduccion.Location = new System.Drawing.Point(0, 0);
            this.btAgregarProduccion.Name = "btAgregarProduccion";
            this.btAgregarProduccion.Size = new System.Drawing.Size(160, 30);
            this.btAgregarProduccion.TabIndex = 0;
            this.btAgregarProduccion.Text = "Agregar Producción";
            this.btAgregarProduccion.UseVisualStyleBackColor = false;
            this.btAgregarProduccion.Click += new System.EventHandler(this.btAgregarProduccion_Click);
            // 
            // lsbProducciones
            // 
            this.lsbProducciones.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lsbProducciones.FormattingEnabled = true;
            this.lsbProducciones.ItemHeight = 12;
            this.lsbProducciones.Location = new System.Drawing.Point(0, 0);
            this.lsbProducciones.Name = "lsbProducciones";
            this.lsbProducciones.Size = new System.Drawing.Size(220, 140);
            this.lsbProducciones.TabIndex = 0;
            // 
            // tlpAgregarProd
            // 
            this.tlpAgregarProd.BackColor = System.Drawing.Color.LightCyan;
            this.tlpAgregarProd.ColumnCount = 2;
            this.tlpAgregarProd.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpAgregarProd.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpAgregarProd.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tlpAgregarProd.Location = new System.Drawing.Point(0, 0);
            this.tlpAgregarProd.Name = "tlpAgregarProd";
            this.tlpAgregarProd.RowCount = 1;
            this.tlpAgregarProd.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpAgregarProd.Size = new System.Drawing.Size(350, 140);
            this.tlpAgregarProd.TabIndex = 0;
            // 
            // btRemoverProduccion
            // 
            this.btRemoverProduccion.BackColor = System.Drawing.Color.AliceBlue;
            this.btRemoverProduccion.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btRemoverProduccion.Location = new System.Drawing.Point(0, 0);
            this.btRemoverProduccion.Name = "btRemoverProduccion";
            this.btRemoverProduccion.Size = new System.Drawing.Size(140, 30);
            this.btRemoverProduccion.TabIndex = 0;
            this.btRemoverProduccion.Text = "Remover Producción";
            this.btRemoverProduccion.UseVisualStyleBackColor = false;
            this.btRemoverProduccion.Click += new System.EventHandler(this.btRemoverProduccion_Click);
            ((System.ComponentModel.ISupportInitialize)(this.dgvSalida)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private void btVolver_Click(object sender, EventArgs e)
        {
            this.handler.backToPrincipal();
        }

        private void btFinalizar_Click(object sender, EventArgs e)
        {
            try
            {
                //if ((dgvSalida != null) && (dgvSalida.Rows.Count != 0)&&(ds.Tables[0].Rows.Count!=0))
                if ((dgvSalida != null) && (dgvSalida.Rows.Count != 0) && (dgvSalida.CurrentCell!=null))
                {

                    if (validarCampos())
                    {
                        MessageBox.Show("DATOS INCOMPLETOS O INCORRECTOS. Asegurese de completar todos los datos necesarios.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }

                    if((lsbProducciones==null)||(lsbProducciones.Items.Count<=0))
                    {
                        MessageBox.Show("NO HAY NINGUNA PRODUCCIÓN CARGADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }

                    //int indx = dgvSalida.CurrentCell.RowIndex;                                                            
                    //string strIdProduccion = dgvSalida[0, indx].Value.ToString();                   
                    //string strCantPaquetesMax = dgvSalida[3, indx].Value.ToString();                   

                    if (verificarDateTimesEnListaProducciones() == false)
                    {
                        MessageBox.Show("UNA O MÁS DE LAS FECHAS FINALES DE PRODUCCIÓN SON INCORRECTAS.\nLA FECHA FINAL DE ENVASADO NO PUEDE SER MAYOR QUE LA FECHA DE SALIDA.", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    
                    DialogResult res = MessageBox.Show("Seguro que quiere crear el remito de salida\n ID Remito: " + lbIdRemito.Text + "\n Fecha Remito de Salida: " + dtpFechaFin.Value.ToString("d") + "\n Cantidad de producciones: " + lsbProducciones.Items.Count, "INFO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                    if (res == DialogResult.Yes)
                    {
                        RemitoSalida remitoSalida = new RemitoSalida();

                        remitoSalida.IdSalida = Convert.ToInt32(lbIdRemito.Text);
                        if (string.IsNullOrWhiteSpace(tbCodigo.Text)) remitoSalida.Codigo = ""; else remitoSalida.Codigo = tbCodigo.Text;
                        remitoSalida.FechaRemitoSalida = dtpFechaFin.Value.Date;
                        //remitoSalida.Produccion = Convert.ToInt32(strIdProduccion);
                        remitoSalida.Usuario = MetodosGenerales.getUsuarioActual();
                        remitoSalida.Codigo = tbCodigo.Text;
                        //remitoSalida.CantidadPaquetes = Convert.ToInt32(tbCantPaquetesSalida.Text);

                        //this.handler.updateFechaFinSalidaEnProduccion(strIdProduccion, dtpFechaFin.Value.Date);

                        this.handler.altaRemitoSalida(remitoSalida);

                        
                        foreach (Produccion prod in lstProducciones)
                        {
                            RemitoSalida_X_Produccion remSalidaXProd = new RemitoSalida_X_Produccion();
                            
                            remSalidaXProd.Id=this.handler.buscarNuevoUltimoIdRemitoSalidaXProduccionEnBD();

                            remSalidaXProd.Produccion = prod.IdProduccion;
                            remSalidaXProd.RemitoSalida = remitoSalida.IdSalida;
                            remSalidaXProd.CantidadProductos = prod.CantidadPaquetes;
                            

                            this.handler.altaRemitoSalida_X_Produccion(remSalidaXProd);
                            this.handler.updateCantidadPaquetesEnvasadosEnProduccionPorId(prod.IdProduccion.ToString(), (prod.CantidadPaquetesMax - prod.CantidadPaquetes).ToString());

                        }

                        MessageBox.Show("El remito de salida fue cargado correctamente", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        dtpFechaFin.Value=System.DateTime.Now;
                       // this.Refresh();
                        this.handler.backToPrincipal();

                    }
                    else
                    {

                    }
                }
                else
                {
                    MessageBox.Show("No hay ninguna producción envasada finalizada en la lista", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (MiException exc)
            {
                MessageBox.Show("No se pudo realizar el remito de salida con la Producción envasada finalizada", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                CLogger.Append("[btFinalizar_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
            }
        }

        private bool verificarDateTimesEnListaProducciones()
        {
            foreach (Produccion prod in lstProducciones)
            {
                int inRes = DateTime.Compare(prod.FechaFin, dtpFechaFin.Value);
                if (inRes > 0)
                {
                    
                    return false;
                }
                
            }

            return true;

        }
        private bool validarCampos()
        {
            return ((lbIdRemito == null) || (string.IsNullOrWhiteSpace(lbIdRemito.Text)) || (dtpFechaFin.Value == null)||(string.IsNullOrWhiteSpace(lbUsuario.Text)));
        }

        private void btBuscarProduccionEnvasadaFin_Click(object sender, EventArgs e)
        {
            if (dgvSalida.CurrentCell==null)
            {
                MessageBox.Show("NO HAY NINGUN VALOR CARGADO EN LA TABLA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            dgvSalida.ClearSelection();
            if (!string.IsNullOrWhiteSpace(tbBuscarEnvasado.Text))
            {
                string searchValue = tbBuscarEnvasado.Text;


                try
                {
                    foreach (DataGridViewRow row in dgvSalida.Rows)
                    {
                        if (row.Cells[0].Value.ToString().Equals(searchValue))
                        {
                            dgvSalida.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                            row.Selected = true;

                            //MessageBox.Show("ENCONTRE: "+inRowFinded, "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            dgvSalida.CurrentCell = row.Cells[0];
                            tbBuscarEnvasado.Clear();
                            break;

                        }

                    }
                }
                catch (Exception exc)
                {
                    tbBuscarEnvasado.Clear();
                    MessageBox.Show("NO SE ENCONTRO LA PRODUCCIÓN ENVASADA FINALIZADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    // CLogger.Append("[btBuscarProducto_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                }
            }
            else
            {
                MessageBox.Show("NO SE INGRESO PRODUCCIÓN ENVASADA FINALIZADA A BUSCAR", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }
        }

        private void dgvProduccionFin_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvSalida.SelectionMode = DataGridViewSelectionMode.CellSelect;
        }

        private void btBuscarEntreFecha_Click(object sender, EventArgs e)
        {

            if (dgvSalida.CurrentCell==null)
            {
                MessageBox.Show("NO HAY NINGUN VALOR CARGADO EN LA TABLA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            dgvSalida.ClearSelection();
            if((dtpBuscarIni.Value==null)||(dtpBuscarFin.Value==null))
            {
                MessageBox.Show("VALORES DE FECHA ERRONEOS", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            //if (dtpBuscarIni.Value > dtpBuscarFin.Value) 
            if (DateTime.Compare(dtpBuscarIni.Value, dtpBuscarFin.Value) > 0)
            {
                MessageBox.Show("LA FECHA PRIMARIA NO PUEDE SER MAYOR QUE LA FECHA SECUNDARIA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            string query = "SELECT pr.ID AS [ID Producción],p.nombre AS [Nombre del Producto],pr.fechaFinEnvasado AS [Fecha Fin de Envasado],pr.cantidadPaquetes AS [Cantidad de Paquetes], pr.observacion AS Observacion1,pr.observacionFinal AS Observación2,e.observacion AS Observación3,e.observacionFin AS Observación4 FROM Produccion AS pr INNER JOIN Producto AS p ON p.ID=pr.producto INNER JOIN Envasado AS e ON e.produccion=pr.ID WHERE pr.fechaFinEnvasado IS NOT NULL AND (pr.fechaFinEnvasado BETWEEN @FECHAINICIO AND @FECHAFIN) AND pr.cantidadPaquetes>'0' ";      
            adapter = SQLclass.obtenerAdapter(query);

            adapter.SelectCommand.Parameters.AddWithValue("@FECHAINICIO", dtpBuscarIni.Value.Date);
            //adapter.SelectCommand.Parameters.AddWithValue("@FECHAFIN", dtpBuscarFin.Value.ToString("MM/dd/yyyy") + " 23:59");

            adapter.SelectCommand.Parameters.AddWithValue("@FECHAFIN", dtpBuscarFin.Value.Date);

            if (adapter != null)
            {
                ds = new DataSet();
                adapter.Fill(ds);
                                
                if ( ds.Tables[0].Rows.Count <= 0)
                {
                    MessageBox.Show("NO SE ENCONTRARON PRODUCCIONES ENVASADAS FINALIZADAS ENTRE ESAS FECHAS", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                }
                else
                {

                    dgvSalida.DataSource = ds.Tables[0];
                }

                
            }
            else
            {
                //CLogger.Append("[ProveedorPanelMostrar] Lista Proveedor mostrar Null: ", CLogger.INFO);      //DEBUG
                MessageBox.Show("ERROR. NO SE ENCONTRARON PRODUCCIONES ENVASADAS FINALIZADAS ENTRE ESAS FECHAS", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.handler.backToPrincipal();
            }
        }


        private void btAgregarProduccion_Click(object sender, EventArgs e)
        {

            if(dgvSalida.CurrentCell==null)
            {
                MessageBox.Show("NO HAY DATOS EN LA TABLA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            int indx = dgvSalida.CurrentCell.RowIndex;
            Produccion produccion = new Produccion();
            produccion.IdProduccion = Convert.ToInt32(dgvSalida[0, indx].Value.ToString());
            string strCantPaquetesMax = dgvSalida[3, indx].Value.ToString();

            string strDataFechaIni = dgvSalida[2, indx].Value.ToString();
            DateTime dtDateIni = (DateTime)dgvSalida[2, indx].Value;


            if (verificarRepeticionItemsEnListBox(produccion.IdProduccion) == false)
            {
                MessageBox.Show("LA PRODUCCIÓN YA SE ENCUENTRA CARGADA. \nPARA CAMBIAR LA CANTIDAD DE PAQUETES DEBERA REMOVER LA PRODUCCIÓN Y VOLVER A AGREGARLA CON EL VALOR CORRECTO", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                tbCantPaquetesSalida.Clear();
                return;
            }

            if ((tbCantPaquetesSalida == null) || (string.IsNullOrWhiteSpace(tbCantPaquetesSalida.Text)))
            {
                MessageBox.Show("DEBE COLOCAR CANTIDAD DE PAQUETES", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                return;
            }

            int distance;
            if (!int.TryParse(tbCantPaquetesSalida.Text, out distance))
            {
                MessageBox.Show("LA CANTIDAD DE PAQUETES DEBE SER UN VALOR NUMÉRICO", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                tbCantPaquetesSalida.Clear();
                return;
            }

            if (string.IsNullOrWhiteSpace(strCantPaquetesMax))
            {
                MessageBox.Show("LA PRODUCCIÓN SELECCIONADA NO POSEE NINGUN PAQUETE", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (Convert.ToInt32(tbCantPaquetesSalida.Text) > Convert.ToInt32(strCantPaquetesMax))
            {
                MessageBox.Show("LA CANTIDAD DE PAQUETES INGRESADOS NO PUEDE SER MAYOR A LA CANTIDAD DE PAQUETES PRODUCIDOS", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                tbCantPaquetesSalida.Clear();
                return;
            }

            produccion.CantidadPaquetes = Convert.ToInt32(tbCantPaquetesSalida.Text);


            string strDataFechaFinEnv = dgvSalida[2, indx].Value.ToString();                    
            DateTime dtDateFinEnv = (DateTime) dgvSalida[2, indx].Value;

            produccion.FechaFin = dtDateFinEnv;
            produccion.CantidadPaquetesMax = Convert.ToInt32( strCantPaquetesMax);

            string strProducto=dgvSalida[1, indx].Value.ToString();                    
            
            string strItem = "ID: " + produccion.IdProduccion.ToString() + " - " + strProducto + " (· " + tbCantPaquetesSalida.Text+" ·) - DT: "+strDataFechaIni;
            
            
            lstProducciones.Add(produccion);
            lsbProducciones.Items.Add(strItem);

            tbCantPaquetesSalida.Clear();
        
        }
        
        private bool verificarRepeticionItemsEnListBox(int id)
        {
            if(lsbProducciones.Items.Count<=0)
            {
                return true;
            }

            int x = 0;
            while(lsbProducciones.Items.Count>x)
            {
                string strProduccion = lsbProducciones.Items[x].ToString();
                int start = strProduccion.IndexOf("ID: ") + 4;
                int length = strProduccion.IndexOf(" - ") - start;

              //  MessageBox.Show("START: "+start+" Lengh: "+length, "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                if (id.ToString() == strProduccion.Substring(start, length))
                {
                    return false;
                }
                x++;
            }

            return true;
          
        }

        private void btRemoverProduccion_Click(object sender, EventArgs e)
        {
             if (lsbProducciones.Items.Count <= 0)
            {
                MessageBox.Show("NO HAY NINGUNA PRODUCCIÓN CARGADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                if((lsbProducciones.SelectedIndex != null)&&(lsbProducciones.SelectedItem!=null))
                {
                    string strItem = lsbProducciones.Items[lsbProducciones.SelectedIndex].ToString();

                    int start = strItem.IndexOf(": ") + 2;
                    int length = strItem.IndexOf(" - ") - start;

                  //  MessageBox.Show("START: " + start + " Lengh: " + length, "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                   
                    foreach (Produccion produccion in lstProducciones)
                    {
                        if (produccion.IdProduccion == Convert.ToInt32(strItem.Substring(start, length))) 
                        {
                            lstProducciones.Remove(produccion);
                            break;
                        }

                    }

                    lsbProducciones.Items.Remove(lsbProducciones.SelectedItem);
                }
                else 
                {
                    MessageBox.Show("NO HAY NINGUNA PRODUCCIÓN SELECCIONADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }

        }
    }
}
